package com.bh.web.controller.order;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.extend.domain.JcOrderBarcode;
import com.bh.extend.service.IJcOrderBarcodeService;
import com.bh.extend.vo.JcOrderBarcodeImportVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 条码与商品对应Controller
 *
 * @author slx
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/order/barcode")
public class JcOrderBarcodeController extends BaseController {

    @Autowired
    private IJcOrderBarcodeService jcOrderBarcodeService;



    /**
     * 查询条码与商品对应列表
     */
    @ApiOperation("查询条码与商品对应列表")
    @PreAuthorize("@ss.hasPermi('order:barcode:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcOrderBarcode jcOrderBarcode)
    {
        startPage();
        List<JcOrderBarcode> list = jcOrderBarcodeService.selectJcOrderBarcodeList(jcOrderBarcode);
        return getDataTable(list);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<JcOrderBarcodeImportVo> util = new ExcelUtil<>(JcOrderBarcodeImportVo.class);
        util.importTemplateExcel(response, "客户自定义条码导入数据");
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<JcOrderBarcodeImportVo> util = new ExcelUtil<>(JcOrderBarcodeImportVo.class);
        List<JcOrderBarcodeImportVo> codeList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = jcOrderBarcodeService.importCode(codeList, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出条码与商品对应列表
     */
    @PreAuthorize("@ss.hasPermi('order:barcode:export')")
    @Log(title = "条码与商品对应", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcOrderBarcode jcOrderBarcode)
    {
        List<JcOrderBarcode> list = jcOrderBarcodeService.selectJcOrderBarcodeList(jcOrderBarcode);
        ExcelUtil<JcOrderBarcode> util = new ExcelUtil<JcOrderBarcode>(JcOrderBarcode.class);
        util.exportExcel(response, list, "条码与商品对应数据");
    }

    /**
     * 导出条码与商品对应列表，目前可以正确下载导出
     */
    @Log(title = "条码与商品对应" , businessType = BusinessType.EXPORT)
    @PostMapping("/exportPdf")
    public AjaxResult exportPdf(JcOrderBarcode jcOrderBarcode) {
        return jcOrderBarcodeService.exportPdf(jcOrderBarcode);
    }


    /**
     * 直接打印条码与商品对应列表
     */
    @Log(title = "条码与商品对应" , businessType = BusinessType.EXPORT)
    @PostMapping("/printPdf")
    public AjaxResult printPdf(HttpServletResponse response, JcOrderBarcode jcOrderBarcode) {
        //打印的同时需要保存结果
        jcOrderBarcodeService.printPdf(response,jcOrderBarcode);
        return AjaxResult.success();
    }

}
